package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.entity .SysUserLike;
import cn.wgx.modules.sys .services.SysUserLikeService;
import cn.wgx.modules.sys.util.UserUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "2.6 用户点赞")
@RestController
@RequestMapping(path = "api/sysUserLike")
public class SysUserLikeController extends BaseController{

    @Resource
    SysUserLikeService service;

//    @ApiOperation(value = "点赞过的列表", notes = "" )
//    @ApiPermission(false)
//    @RequestMapping(path = "list", method = RequestMethod.GET)
//    public Map<String, Object> getList(
//            @ModelAttribute SysUserLike record,
//			@RequestParam(value = "page", required = false, defaultValue = "1")Integer page,
//			@RequestParam(value = "pageSize", required = false, defaultValue = "20")Integer pageSize,
//            @ApiParam(value = "是否倒序,默认是")@RequestParam(defaultValue = "true", required = false)Boolean desc
//        ) {
//		return returnData(PageHelper.startPage(page, pageSize), service.getList(record, desc));
//    }

    @ApiOperation(value = "查看某资源点赞人员列表", notes = "必须参数: res_id, type" )
    @ApiPermission(false)
    @RequestMapping(path = "listUser", method = RequestMethod.GET)
    public Map<String, Object> getListByLike(
            @RequestParam Integer res_id,
            @RequestParam Integer type,
            @RequestParam(value = "page", required = false, defaultValue = "1")Integer page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "20")Integer pageSize
    ) {
        return returnData(PageHelper.startPage(page, Math.min(Math.abs(pageSize), maxPageSize)), service.getUserByLike(page, pageSize));
    }
	
    @ApiOperation(value = "新建点赞", notes = "必须参数: type(收藏类型: 1课程,2作业,3笔记), res_id(素材ID)" )
    @ApiPermission(false)
    @RequestMapping(path = "add", method = RequestMethod.POST)
    public Map<String, Object> insertOne(@RequestBody SysUserLike record) {
        if(record == null){
            return  returnErrorMap("参数错误");
        }
        Integer type = record.getType();
        Integer res_id = record.getRes_id();
        if(type== null || Validation.isNullOrZero(res_id)  || !(type == 1 || type == 2 || type ==3)){
            return  returnErrorMap("类型错误");
        }
        SysUser user = UserUtil.getUser();
        if(user == null){
            return returnErrorMap("重新登录");
        }

        if(service.getOne(user.getUser_id(), type, res_id) != null){
            return returnErrorMap("已点赞");
        }
        if(!service.checkHas(type, res_id)){
            return returnErrorMap("资源ID错误.");
        }
        record.setUser_id(user.getUser_id());
        service.insertOne(record);
        return returnData(record.get_result());
    }


    @ApiOperation(value = "取消点赞", notes = "必须参数: type, res_id" )
    @ApiPermission(false)
    @RequestMapping(path = "del/{type}/{res_id}", method = RequestMethod.DELETE)
    public Map<String, Object> deleteOne(@PathVariable("type") Integer type,@PathVariable("res_id") Integer res_id) {
        SysUser user = UserUtil.getUser();
        return returnData(service.deleteOne(user.getUser_id(),type,res_id));
    }

	

}